<template>
    <el-form v-if="!type" ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions :column="4" border >
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">申请人　　</div>
                </template>
                <div ><el-text>{{ ruleForm.id ? ruleForm.user.name : user.name }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item :span="2">
                <template #label>
                    <div class="cell-item">项目名称　</div>
                </template>
                <div ><component :is="projectSelect" :data=ruleForm.project_id @onProjectSelect="onProjectSelect" /></div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">申请金额　</div>
                </template>
                <div ><el-input class="no-border-input" v-model="ruleForm.apply_price" @change="onChange" /></div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">对方性质　</div>
                </template>
                <div >
                    <el-select style="width: 200px" v-model="ruleForm.nature">
                        <el-option label="备用金" value="备用金" />
                        <el-option label="借款" value="借款" />
                    </el-select>
                </div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">支付方式　</div>
                </template>
                <div >
                    <el-select style="width: 200px" v-model="ruleForm.pay_type">
                        <el-option label="对公转账" value="对公转账" />
                        <el-option label="对私转账" value="对私转账" />
                        <el-option label="数字人民币" value="数字人民币" />
                        <el-option label="电子货币" value="电子货币" />
                        <el-option label="现金支票" value="现金支票" />
                        <el-option label="其它" value="其它" />
                    </el-select>
                </div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">支付公司　</div>
                </template>
                <div >
                    <el-select style="width: 200px" v-model="ruleForm.pay_company">
                        <el-option label="华靖" value="华靖" />
                        <el-option label="华雨" value="华雨" />
                        <el-option label="博创" value="博创" />
                        <el-option label="瑞智腾" value="瑞智腾" />
                    </el-select>
                </div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">银行手续费</div>
                </template>
                <div ><el-input class="no-border-input" v-model="ruleForm.bank_charges" /></div>
            </el-descriptions-item>
            <el-descriptions-item :span="4">
                <template #label>
                    <div class="cell-item">合计　　　</div>
                </template>
                <div ><el-text>{{ ruleForm.china_price }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item :span="4">
                <template #label>
                    <div class="cell-item">用途　　　</div>
                </template>
                <div ><el-input class="no-border-input" v-model="ruleForm.purpose" type="textarea" :autosize="{ minRows: 4 }" /></div>
            </el-descriptions-item>
            <el-descriptions-item :span="4" label=""><div class="text-center"><el-text>收款账号</el-text></div></el-descriptions-item>
            <el-descriptions-item :span="2" label="账号名称"><el-input class="no-border-input" v-model="user.name" /></el-descriptions-item>
            <el-descriptions-item :span="2" label="开户行　"><el-input class="no-border-input" v-model="user.bank" /></el-descriptions-item>
            <el-descriptions-item :span="2" label="账号　　"><el-input class="no-border-input" v-model="user.bank_number" /></el-descriptions-item>
            <el-descriptions-item :span="2" label="行号　　"><el-input class="no-border-input" v-model="ruleForm.bank_no" /></el-descriptions-item>
            <el-descriptions-item :span="4" label="备注　　"><el-input class="no-border-input" v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" /></el-descriptions-item>
        </el-descriptions>
        <el-divider />
        <div class="text-center">
            <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
            <el-button @click="printElement('')"> 打印预览 </el-button>
        </div>
    </el-form>

    <div v-else>
        <div id="print" class="print">
            <h2 class="text-center">备用金（借款）申请单</h2>
            <el-descriptions :column="3" border >
                <el-descriptions-item label="申请人　　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.user.name }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="项目名称　" label-class-name="my-label" class-name="my-content"><div><el-text>{{ ruleForm.project.title }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="申请金额　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.apply_price }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="对方性质　" label-class-name="my-label" class-name="my-content">
                    <div class="w-f"><el-text>{{ ruleForm.nature }}</el-text></div>
                </el-descriptions-item>
                <el-descriptions-item label="支付方式　" label-class-name="my-label" class-name="my-content">
                    <div class="w-f"><el-text>{{ ruleForm.pay_type }}</el-text></div>
                </el-descriptions-item>
                <el-descriptions-item label="支付公司　" label-class-name="my-label" class-name="my-content">
                    <div class="w-f"><el-text>{{ ruleForm.pay_company }}</el-text></div>
                </el-descriptions-item>
                <el-descriptions-item label="银行手续费" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.bank_charges }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="用途　　　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.purpose }}</el-text></div></el-descriptions-item>
            </el-descriptions>
            <h4 class="text-center mt10">收款账号</h4>
            <el-descriptions class="mt10" :column="2" border >
                <el-descriptions-item label="账号名称　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.name }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="开户行　　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.bank }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="账号　　　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.bank_number }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="行号　　　" label-class-name="my-label" class-name="my-content"><div class="w-f"><el-text>{{ ruleForm.bank_no }}</el-text></div></el-descriptions-item>
                <el-descriptions-item label="备注　　　" label-class-name="my-label" class-name="my-content"><div class="w-f" style="height: 60px;"><el-text>{{ ruleForm.remarks }}</el-text></div></el-descriptions-item>
            </el-descriptions>
            <el-descriptions class="mt20" :column="4" size="small">
                <el-descriptions-item label="项目部：(实际申请人）" label-class-name="my-label" class-name="my-content">　　</el-descriptions-item>
                <el-descriptions-item label="综合管理部" label-class-name="my-label" class-name="my-content">　　</el-descriptions-item>
                <el-descriptions-item label="总经理" label-class-name="my-label" class-name="my-content">　　</el-descriptions-item>
                <el-descriptions-item label="会计" label-class-name="my-label" class-name="my-content">　　</el-descriptions-item>
            </el-descriptions>
        </div>
        <div class="text-center mt30">
            <el-button @click="printElement('print')"> 打印 </el-button>
            <el-button type="info" @click="type = false"> 返回 </el-button>
        </div>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../util/request';
import projectSelect from "@/components/projectSelect.vue";
import { PrinterService } from '../util/printer'
import func from '../util/function';
let user:any = sessionStorage.getItem('user')
user = JSON.parse(user)
const type = ref<boolean>(false)
const printer = new PrinterService()
const loading = ref<boolean>(false)
interface RuleForm {
    id:number
    project_id:number
    user_id:number
    apply_date: string
    apply_price: number
    china_price: string
    back_borrow: number
    audit_price: number
    nature: string
    pay_type:string
    price: number
    receive_type: string
    details:any
    pay_company: string
    bank_charges: string
    name: string
    bank: string
    bank_number: string
    bank_no: string
    remarks: string
    purpose:string
    user:any
    project:any
    
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id:0,
    project_id:0,
    user_id:0,
    apply_date: '',
    price: 0,
    back_borrow: 0,
    audit_price: 0,
    china_price:'',
    nature: '/',
    pay_type:'/',
    apply_price: 0,
    receive_type: '/',
    details:'',
    pay_company: '/',
    bank_charges: '/',
    name: '/',
    bank: '/',
    bank_number: '/',
    bank_no: '/',
    remarks: '/',
    purpose:'/',
    user:{},
    project:{}
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/imprest/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
        
    })
}

const onChange = () => {
    ruleForm.value.china_price = func.numberToChinese(ruleForm.value.apply_price)
}



const props = defineProps<{
    data:any
    type:boolean
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    type.value = props.type
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
   
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/imprest',ruleForm.value)
            }else{
                request = http.put('/imprest',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const printElement = (elementId:string) => {
    if(elementId){
        printer.printElement(elementId, { title: '　' })
        return;
    }
    type.value = true
}


const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])

const onProjectSelect = (data:number) => {
    ruleForm.value.project_id = data
}
</script>
<style scoped>
.print{width: 210mm;size: A4;margin:0px auto;padding: 0px 15px 0px 15px;}
.cell-item{width:80px;text-align: center;}
.w-f{width: 88px;}
:deep(.my-label) {
    width: 80px;
  padding: 2px 4px !important;
  font-size: .8rem !important;
}

:deep(.my-content) {
    padding: 2px 4px !important;
}
/* 全局移除 el-input 边框 */
.no-border-input {
/* 覆盖 Element Plus 的 CSS 变量 */
	--el-input-border-color: transparent;
	--el-input-focus-border-color: transparent;
	--el-input-hover-border-color: transparent;
}

/* 配合移除阴影 */
:deep(.no-border-input .el-input__inner) {
	box-shadow: none;
}
</style>
